package tuiLayer;
import modelLayer.*;
import controllerLayer.*;

import java.util.*;

public class DvdUI {
	private static DvdCTR dvdCtr ;
	private static Scanner scanner = new Scanner(System.in);
	
	public static void menu(DvdCTR dvdCTR){
		dvdCtr = dvdCTR;
		boolean quit = false;
		while(!quit){
			printOptions();
			int input = prompt("Choose!");
			switch(input) {
				case 1:
					createDVD();
					System.out.println("Done!");
					break;
				case 2:
					createCpy();
					break;
				case 3:
					updateDvd();
					break;
				case 4:
					updateCpy();
					break;	
				case 5:
					int barcode = prompt("Enter the barcode of the DVD:");
					dvdCtr.printDvdAndItsCopys(barcode);
					break;
				case 6:
					dvdCtr.printAllDvdss();
					break;
				case 7:
					showSpecificCpy();
					break;
				case 8:
					barcode = prompt("Enter the barcode of the DVD you want to delete:");
					DVD dvd = dvdCtr.findDvd(barcode);
					if(dvd!=null)
					{
					dvdCtr.deleteDvd(dvd);
				}
					else
					{
					System.out.println("Enter a valid barcode");
				}
					break;
				case 9:
					deleteCpy();
					break;
				case 10:
					quit = true;
					break;
				default:
					System.out.println("Sorry, but that's not a choice!");
					printOptions();
			}
		}
		
	}
	private static int prompt(String prompt){
		System.out.print(" " + prompt + " ");
		int input = scanner.nextInt();
		return input;
	}
	
	private static double promptD(String prompt){
		System.out.print(" " + prompt + " ");
		double input = scanner.nextInt();
		return input;
	}
	
	private static String prompt2(String prompt) {
        System.out.print( prompt + " ");
        String input = scanner.next();
        return input.toLowerCase();
    }
	
	private static void printOptions(){
		System.out.println("***DVDMenu***");
		System.out.println("1. Create DVD");
		System.out.println("2. Create a Copy to a specific DVD");
		System.out.println("3. Change a DVD's Info");
		System.out.println("4. Change a Copy's Info");
		System.out.println("5. Show a specific DVD");
		System.out.println("6. Show all DVDs");
		System.out.println("7. Show a specific Copy of a specific DVD");
		System.out.println("8. Delete DVD");
		System.out.println("9. Delete Copy of a specific DVD");
		System.out.println("10. Back to MainMenu");
	}
	
	private static void createDVD(){
		String title, artist, publicationDate;
		int barcode = prompt("Enter the barcode of the new DVD:");
		title = prompt2("Enter the title of the new DVD:");
		artist = prompt2("Enter the artist of the new DVD:");
		publicationDate = prompt2("Enter the publication date of the new DVD:");
		dvdCtr.createDvd(barcode, title, artist, publicationDate);
	}
	
	private static void createCpy(){
		String serialNumber, purchaseDate;
		int barcode = prompt("Enter the barcode of the Dvd in which you want to add the Copy");
		if(dvdCtr.findDvd(barcode)!=null)
		{
		serialNumber = prompt2("Enter the serial number of the new Copy");
		purchaseDate = prompt2("Enter the purchase date of the new Copy");
		double purchasePrice = promptD("Enter the purchase price of the new Copy");
		dvdCtr.createCpy(serialNumber, purchaseDate, purchasePrice, barcode);
}
else
{
    System.out.println("Enter a valid barcode");
}
	}
	/**
	 * This method updates Dvd's information.
	 * You enter the DVD's barcode.
	 * If the barcode is valid you can enter the new parameter:
	 * @param title
	 * @param arist
	 * @param publicationDate
	 * If it is not the system prints message:"Enter a valid barcode".
	 */
	 private static void updateDvd(){
		String title, artist, publicationDate;
		int barcode = prompt("Enter the barcode of the DVD you want to change:");
	 	if(dvdCtr.findDvd(barcode)!=null)
	 	{
		DVD dvd = dvdCtr.findDvd(barcode);
	 	title = prompt2("Enter the title of the new DVD:");
	 	artist = prompt2("Enter the artist of the new DVD:");
		publicationDate = prompt2("Enter the publication date of the new DVD:");
	 	dvdCtr.updateDvd(dvd, title, artist, publicationDate);
    }
    else
    {
     System.out.println("Enter a valid barcode");   
    }
	}
	/**
	 * This method updates Copy's information
	 * You enter the DVD's barcode and Copy's serial number you want to change.
	 * If barcode is invalid system prints message:"Enter a valid barcode".
	 * If serial number is invalid system prints message:"Enter a valid serial number".
	 * If they both are valid you can enter the new parameter:
	 * @param purchaseDate
	 * @param purchasePrice
	 */
	 private static void updateCpy(){
		String purchaseDate;
		double purchasePrice;
		int barcode = prompt("Enter the barcode of the DVD in which you want to change the copy:");
		if(dvdCtr.findDvd(barcode)!=null)
		{
		String serialNumber = prompt2("Enter the serial number of the Copy you want to change:");
		if(dvdCtr.findCpy(barcode, serialNumber)!=null)
		{
		Copy cpy = dvdCtr.findCpy(barcode, serialNumber);
		purchaseDate = prompt2("Enter the new purchase date to the Copy:");
		purchasePrice = promptD("Enter the new price of the Copy(double)");
		dvdCtr.updateCpy(cpy, purchaseDate, purchasePrice );
}
else
{
 System.out.println("Enter a valid serial number");   
}
}
else
{
 System.out.println("Enter a valid barcode");   
}

	}
	/**
	 * This method shows you the specific copy you want to see.
	 * You enter the barcode of the Copy's DVD and the Copy's serial number you want to see.
	 * If the serial number is invalid system prints message:"Enter a valid serial number".
	 * If the barcode is invalid system prints message:"Enter a valid barcode".
	 */
	private static void showSpecificCpy(){
		int barcode = prompt("Enter the barcode of the Copy's DVD you want to see:");
		if(dvdCtr.findDvd(barcode)!=null)
		{
		String serialNumber = prompt2("Enter the serial number of the Copy you want to see:");
		if(dvdCtr.findCpy(barcode, serialNumber)!=null)
		{
		dvdCtr.findCpy(barcode, serialNumber).showCopy();
}
else
System.out.println("Enter a valid serial number");
}
else
System.out.println("Enter a valid barcode");
	}
	/**
	 * This method deletes specific Copy you want to delete.
	 * You enter the barcode of the Copy's DVD and the Copy's serial number you want to delete.
	 * If the serial number is invalid system prints message:"Enter a valid serial number".
	 * If the barcode is invalid system prints message:"Enter a valid barcode".
	 */
	
	private static void deleteCpy(){
		int barcode = prompt("Enter the barcode of the Copy's DVD you want to delete:");
		if(dvdCtr.findDvd(barcode)!=null)
		{
		String serialNumber = prompt2("Enter the serial number of the Copy you want to delete:");
		if(dvdCtr.findCpy(barcode, serialNumber)!=null)
		{
		dvdCtr.deleteCpy(barcode, serialNumber);
        }
        else
        System.out.println("Enter a valid serial number");
        }
        else
        System.out.println("Enter a valid barcode");
	}
	
}
